package com.bangdream.michelia.application;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.Application;
import android.content.Context;
import android.os.Build;
import android.os.StrictMode;

import com.bangdream.michelia.sql.greendao.GreenDaoManager;
import com.bangdream.michelia.utils.SPUtils;
import com.mob.MobSDK;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.DefaultRefreshFooterCreater;
import com.scwang.smartrefresh.layout.api.DefaultRefreshHeaderCreater;
import com.scwang.smartrefresh.layout.api.RefreshFooter;
import com.scwang.smartrefresh.layout.api.RefreshHeader;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.constant.SpinnerStyle;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.bangdream.michelia.R;

import java.util.LinkedList;
import java.util.List;


/**
 * Created by Administrator on 2016/6/22.
 * 定义全局context
 */
public class MainApplication extends Application {
	private final static String TAG = "MainApplication";
    private static Context mContext;
	private static List<Activity> mList = new LinkedList<Activity>();
	private static MainApplication instance;
	private static boolean isChangeDomain=false;
	private static int width,height;                                               //屏幕的宽高
	//private static DaoSession daoSession;
	public MainApplication() {

	}
	static {//static 代码段可以防止内存泄露
		//设置全局的Header构建器
		SmartRefreshLayout.setDefaultRefreshHeaderCreater(new DefaultRefreshHeaderCreater() {
			@Override
			public RefreshHeader createRefreshHeader(Context context, RefreshLayout layout) {
				//layout.setPrimaryColorsId(R.color.colorPrimary, android.R.color.white);//全局设置主题颜色
				layout.setPrimaryColorsId(R.color.gray, android.R.color.white);//全局设置主题颜色
				return new ClassicsHeader(context).setSpinnerStyle(SpinnerStyle.Translate);//指定为经典Header，默认是 贝塞尔雷达Header
			}
		});
		//设置全局的Footer构建器
		SmartRefreshLayout.setDefaultRefreshFooterCreater(new DefaultRefreshFooterCreater() {
			@Override
			public RefreshFooter createRefreshFooter(Context context, RefreshLayout layout) {
				//指定为经典Footer，默认是 BallPulseFooter
				return new ClassicsFooter(context).setSpinnerStyle(SpinnerStyle.Translate);
			}
		});
	}
	@Override
    public void onCreate() {
        super.onCreate();
		init();
	}

	private void init() {
		instance = this;
		mContext = getApplicationContext();
		//mob社会化分享初始化
		MobSDK.init(this);
		//greenDao数据库初始化
		GreenDaoManager.getInstance();
		/**
		 * 配置数据库
		 */
		//GreenDaoManager.getInstance();
		//解决android7.0拍照问题
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
			StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
			StrictMode.setVmPolicy(builder.build());
		}
	}

	public String getDomain(){
		return (String) SPUtils.get(mContext, "doMain", "inesa");
	}

//	public Boolean isChangeDomain(){
//		isChangeDomain=true;
//		return isChangeDomain;
//	}


	public  boolean isIsChangeDomain() {
		return isChangeDomain;
	}

	public void setIsChangeDomain(boolean isChangeDomain) {
		MainApplication.isChangeDomain = isChangeDomain;
	}

	public static String getCurProcessName(Context context) {
		int pid = android.os.Process.myPid();
		ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
		for (ActivityManager.RunningAppProcessInfo appProcess : activityManager.getRunningAppProcesses()) {
			if (appProcess.pid == pid) {
				return appProcess.processName;
			}
		}
		return null;
	}
    /**
     * 获取Context.
     *
     * @return
     */
    public static Context getContext() {
        return mContext;
    }

    @Override
    public void onLowMemory() {
        super.onLowMemory();
		System.gc();
    }

	public synchronized static MainApplication getInstance() {
		if (null == instance) {
			instance = new MainApplication();
		}
		return instance;
	}

	// add Activity
	public static void addActivity(Activity activity) {
		mList.add(activity);
	}

	public static void exit() {
		try {
			for (Activity activity : mList) {
				if (activity != null)
					activity.finish();
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			System.exit(0);
		}
	}

	public int getWidth() {
		return width;
	}

	public void setWidth(int width) {
		this.width = width;
	}

	public int getHeight() {
		return height;
	}

	public void setHeight(int height) {
		this.height = height;
	}
}
